package com.saturn.system;

import java.util.List;

import org.json.JSONObject;

import com.saturn.app.db.DymaticCondition;
import com.saturn.app.db.ListData;
import com.saturn.app.db.ORMapping;
import com.saturn.app.db.ResultORMapping;
import com.saturn.app.db.SimpleDaoTemplate;
import com.saturn.app.exception.BSException;

public class DictTest {

	private String id;
	private String test1;
	private String test2;
	private String test3;
	private String test4;
	private String test1Name;
	private String test2Name;
	private String test3Name;
	private String test4Name;

	private static ORMapping<DictTest> mapping = new ResultORMapping<DictTest>();

	public static ListData<DictTest> getDicts(DictTest dict, String start,
			String offset, String orderBy, String order) {

		DymaticCondition addCondition = new DymaticCondition()
				.addSimpleCondition(dict, "test1", "test2", "test3", "test4","test1Name","test2Name","test3Name","test4Name");

		int total = SimpleDaoTemplate.queryCount(
				"SELECT count(*) FROM app_dict_test WHERE 1 = 1", addCondition);

		List<DictTest> list = SimpleDaoTemplate.query(
				"SELECT * FROM app_dict_test WHERE 1 = 1", addCondition
						.addCondition("ORDER BY {0} {1}", orderBy, order)
						.addCondition("LIMIT {0}, {1}", start, offset),
				mapping, DictTest.class);

		return new ListData<DictTest>(total, list);
	}

	public static int add(DictTest dict) {
		if (get(dict.getId()) != null) {
			throw new BSException("数据字典ID重复,id=" + dict.getId());
		}

		return SimpleDaoTemplate.update(
				"INSERT INTO app_dict_test (id,test1,test1Name,test2,test2Name,test3,test3Name,test4,test4Name) VALUES(?,?,?,?,?,?,?,?,?)", dict.getId(), dict
						.getTest1(),dict.getTest1Name(),dict.getTest2(),dict.getTest2Name(),dict.getTest3(),dict.getTest3Name(),dict.getTest4(),dict.getTest4Name());
	}

	public static DictTest get(String id) {
		return SimpleDaoTemplate.queryOne("SELECT * FROM app_dict_test WHERE 1 = 1",
				new DymaticCondition().addCondition("and id = '?'", id),
				mapping, DictTest.class);
	}

	public static int edit(DictTest dict) {
		return SimpleDaoTemplate
				.update(
						"UPDATE app_dict_test SET  test1 = ?, test1Name = ?,test2 = ?, test2Name = ?,test3 = ?, test3Name = ?,test4 = ?, test4Name = ? WHERE id = ?",
						 dict.getTest1(), dict.test1Name,dict.getTest2(), dict.test2Name,dict.getTest3(), dict.test3Name,dict.getTest4(), dict.test4Name, dict
								.getId());
	}

	public static int remove(final String id) {
		return SimpleDaoTemplate
				.update("DELETE FROM app_dict_test WHERE id = ?", id);
	}

	public static void removes(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				remove(id);
			}
		}
	}

	public DictTest() {
	}

	public String getTest1() {
		return test1;
	}

	public void setTest1(String test1) {
		this.test1 = test1;
	}

	public String getTest2() {
		return test2;
	}

	public void setTest2(String test2) {
		this.test2 = test2;
	}

	public String getTest3() {
		return test3;
	}

	public void setTest3(String test3) {
		this.test3 = test3;
	}

	public String getTest4() {
		return test4;
	}

	public void setTest4(String test4) {
		this.test4 = test4;
	}

	public String getTest1Name() {
		return test1Name;
	}

	public void setTest1Name(String test1Name) {
		this.test1Name = test1Name;
	}

	public String getTest2Name() {
		return test2Name;
	}

	public void setTest2Name(String test2Name) {
		this.test2Name = test2Name;
	}

	public String getTest3Name() {
		return test3Name;
	}

	public void setTest3Name(String test3Name) {
		this.test3Name = test3Name;
	}

	public String getTest4Name() {
		return test4Name;
	}

	public void setTest4Name(String test4Name) {
		this.test4Name = test4Name;
	}

	public DictTest(String test1, String test2, String test3, String test4,
			String test1Name, String test2Name, String test3Name,
			String test4Name,String id) {
		super();
		this.id = id;
		this.test1 = test1;
		this.test2 = test2;
		this.test3 = test3;
		this.test4 = test4;
		this.test1Name = test1Name;
		this.test2Name = test2Name;
		this.test3Name = test3Name;
		this.test4Name = test4Name;
	}

	@Override
	public String toString() {
		return new JSONObject(this).toString();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
